@inject IFileVersionProvider Version
@inject IHttpContextAccessor HttpContextAccessor

<script @attributes="AdditionalAttributes" nonce="@Nonce"> @ChildContent </script>

@code {
    
    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object> AdditionalAttributes { get; set; } = new();
    
    [Parameter]
    public RenderFragment? ChildContent { get; set; }
    
    protected override void OnInitialized()
    {
        if (AdditionalAttributes.ContainsKey("src"))
        {
            AdditionalAttributes["src"] = Version.Get(AdditionalAttributes["src"].ToString()!);
        }
    }

    public string? Nonce
    {
        get
        {
            if (HttpContextAccessor.HttpContext?.Items == null)
            {
                return null;
            }
            
            if (HttpContextAccessor.HttpContext!.Items.TryGetValue("csp-nonce", out var nonce))
            {
                return nonce?.ToString();
            }

            return null;
        }
    }
}